package cn.kpi.service.pingyou.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.kpi.dao.pingyou.NdkhpsDao;
import cn.kpi.entity.pingyou.ByUserEntity;
import cn.kpi.entity.pingyou.NdkhStatsEntity;
import cn.kpi.entity.pingyou.UserNdkhEntity;
import cn.kpi.service.pingyou.INdkhpsService;


@Service
public class NdkhpsServiceImpl implements INdkhpsService {
	
	@Autowired
	private NdkhpsDao ndkhpsDao;
	
	public List<Map> GetPjOrgSumList(String where,int year)throws Exception{
		try{
			return ndkhpsDao.GetPjOrgSumList(where,year);
			
		}catch(Exception ex){
			return null;
		}
	}
	
	public List<UserNdkhEntity> GetNdkhpsListByWhere(String where,String realname,int year,int beginrow,int endrow)throws Exception{
		try{
			return ndkhpsDao.GetNdkhpsListByWhere(where,realname,year,beginrow,endrow);
			
		}catch(Exception ex){
			return null;
		}
	}
	
	public int GetNdkhpsCountByWhere(String where,String realname, int year)throws Exception{
		try{
			return ndkhpsDao.GetNdkhpsCountByWhere(where,realname,year);
			
		}catch(Exception ex){
			return -1;
		}
	}
	@Transactional
	public int InsertNdkhDetail(ByUserEntity entity)throws Exception{
		try{
			int result=0;
			ByUserEntity byuserentity=ndkhpsDao.GetEntityByuserid(entity.getByuserid(),entity.getOrgid(),entity.getYear());
			result+=ndkhpsDao.DeleteNdkhDetail(entity.getOrgid(),entity.getYear());
			result+=ndkhpsDao.DeleteNdkhOrg(entity.getOrgid(),entity.getYear());
			
			result+=ndkhpsDao.InsertOrgStatus(byuserentity);
			result+=ndkhpsDao.InsertNdkhDetail(byuserentity);
			
			return result;
		}catch(Exception ex){
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物 
			return -1;
		}
	}
	
	public UserNdkhEntity GetNdkhpsEntityByUserid(long ndkhpsid)throws Exception{
		try{
			return ndkhpsDao.GetNdkhpsEntityByUserid(ndkhpsid);
			
		}catch(Exception ex){
			return null;
		}
	}
	public int UpdateNdkhDetail(long ndkhpsid,String ndkhpsjg,String remark)throws Exception{
		try{
			return ndkhpsDao.UpdateNdkhDetail(ndkhpsid,ndkhpsjg,remark);
			
		}catch(Exception ex){
			return -1;
		}
	}
	@Transactional
	public int UpdateNdkhOrg(long orgstatustjid,int flag,String remark,long byuserid,long orgid,int year)throws Exception{
		try{
			ByUserEntity byuserentity=ndkhpsDao.GetEntityByuserid(byuserid,0,0);
			NdkhStatsEntity entity=new NdkhStatsEntity();
			entity.setOrgstatustjid(orgstatustjid);
			entity.setRemark(remark);
			entity.setUserid(byuserid);
			entity.setSzdw(byuserentity.getByuseridszdw());
			entity.setSzdwname(byuserentity.getByuseridszdwname());
			entity.setRsgxszdw(byuserentity.getByuseridrsgxszdw());
			entity.setRsgxszdwname(byuserentity.getByuseridrsgxszdwname());
			int result=0;
			switch (flag) {
			case 1://提交
				result+=ndkhpsDao.UpdateNdkhOrgTj(entity);
				break;
			case 2://归档 
				result+=ndkhpsDao.UpdateNdkhOrgGd(entity);
				break;
			case 3://驳回
				result+=ndkhpsDao.UpdateNdkhOrgBh(entity);
				break;
			}
			result+=ndkhpsDao.UpdateNdkhDetailStatus(orgid,year,flag);
			
			return result;
		}catch(Exception ex){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物 
			return -1;
		}
		
	}

}
